//
//  CSGXBigModel.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/10/26.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXBigModel.h"
#import "TCommodityCategoryDAL.h"

@implementation CSGXBigModel


- (void)setValue:(id)value forKey:(NSString *)key {
    
    if ([key isEqualToString:@"caFirstType"]) {
        key = @"name";
    }
    [super setValue:value forKey:key];
}

+ (instancetype)modelWithDict:(NSDictionary *)dict {
    CSGXBigModel * model = [[self alloc] init];
    [model setValuesForKeysWithDictionary:dict];
    
    return model;
}

+ (NSArray<TCommodityCategoryModel *> *)loadCategorysWithWarehouseNo:(NSString *)warehouseNo {
    warehouseNo = @"CK001";
    NSArray<TCommodityCategoryModel *> * arrFirst = [TCommodityCategoryDAL loadCoCategoryModelFirstWithWarehouseNo:warehouseNo];

    return arrFirst;
}

///test
+ (NSArray *)getTvsOriginData:(NSArray *)arr1 arr2:(NSArray *)arr2 arr3:(NSArray *)arr3 {
    
    NSMutableArray<NSDictionary *> * bigArr = [NSMutableArray array];
    // first
    for (int i = 0; i < arr1.count; i++) {
        
        NSMutableArray<NSDictionary *> *smallArr = [NSMutableArray array];
        // second
        for (int j = 0; j < arr2.count; j++) {
            
            NSMutableArray<NSDictionary *> * commArr = [NSMutableArray array];
            // third
            for (int k = 0; k < arr3.count; k++) {
                NSDictionary * commDict = @{@"commName": arr3[k][@"caThirdType"]};
                [commArr addObject:commDict];
            } // for comm end
            
            NSDictionary * smallDict = @{@"smallName": arr2[j][@"caSecondType"],
                                         @"commArr": commArr};
            [smallArr addObject:smallDict];
        } // for small end
        
        NSDictionary * bigDict = @{@"bigName": arr1[i][@"caFirstType"],
                                   @"smallArr": smallArr};
        [bigArr addObject:bigDict];
        
    } // for big end
    
    // 将bigArr写入plist
//    NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//    NSString * filePath = [path stringByAppendingPathComponent:@"commodity.plist"];
//    if ([bigArr writeToFile:filePath atomically:YES]) {
//        NSLog(@"%@", filePath);
//    }
    
    return bigArr.copy;
}

+ (NSArray<CSGXBigModel *> *)arrWithWarehouseNo:(NSString *)warehouseNo {
    NSArray * dataArr = [self loadCategorysWithWarehouseNo:warehouseNo];
    if (dataArr.count == 0) {
        return nil;
    }
    
    NSMutableArray * mArr = [NSMutableArray array];
    for (NSDictionary *dict in dataArr) {
        [mArr addObject:[self modelWithDict:dict]];
    }
    
    return mArr.copy;
}

///test
+ (NSArray<CSGXBigModel *> *)arrWithPlistName_1:(NSString *)plistName {
    NSMutableArray * mArr = [NSMutableArray array];
    
    NSString * path = [[NSBundle mainBundle] pathForResource:plistName ofType:nil];
    
    NSArray * dataArr = [NSArray arrayWithContentsOfFile:path];
    for (NSDictionary *dict in dataArr) {
        [mArr addObject:[self modelWithDict:dict]];
    }
    
    return mArr.copy;
}

@end
